<template>
	<u-popup ref='areaRef' v-model="areaShow" mode="bottom" length="40%" safe-area-inset-bottom
		:mask-close-able='false'>
		<view class="area_content">
			<view class="area_content_top">
				<scroll-view scroll-y="true" class="area_content_item">
					<view class="area_content_item_content" :style="{color:index == provinceId?'red':''}"
						v-for="(item,index) in areaList" :key='index' @click="onSetindex('provinceId',index,item)">
						{{item.name}}
					</view>
				</scroll-view>


				<scroll-view scroll-y="true" class="area_content_item">
					<view class="area_content_item_content" v-for="(item,index) in cityList" :key='index'
						:style="{color:index == cityId?'red':''}" @click="onSetindex('cityId',index,item)">
						{{item.name}}
					</view>
				</scroll-view>
				<scroll-view scroll-y="true" class="area_content_item">
					<view class="area_content_item_content" v-for="(item,index) in regionList" :key='index'
						:style="{color:index == regionId?'red':''}" @click="onSetindex('regionId',index,item)">
						{{item.name}}
					</view>
				</scroll-view>
			</view>
			<view class="sumbit_btn">
				<u-button type="error" @click="areaShow=false" style="flex: 1; margin-right: 30rpx;">取消</u-button>
				<u-button type="success" @click="onSuccessArea" style="flex: 1;">确定</u-button>
			</view>
		</view>

	</u-popup>
</template>

<script>
	export default {


		data() {
			return {
				addressObj: {
					province: {}, //省
					city: {}, //市
					region: {}, //区
				},
				cityList: [], //市
				regionList: [], //区
				regionId: 0,
				provinceId: 0,
				cityId: 0,
				areaList: [],
				areaShow: false,
			};
		},

		methods: {

			onSuccessArea() {
				this.areaShow = false
				this.$emit('callback', this.addressObj)
			},
			onSetAddress(key, value) {
				this.$set(this.addressObj, key, value)
			},

			onSetindex(typeName, index, item) {
				this[typeName] = index
				let {
					areaList,
					provinceId,
					regionId,
					cityId
				} = this


				let {
					code,
					name
				} = item

				let dataObj = {
					label: name,
					value: code
				}



				if (typeName == 'provinceId') { //省
					this.regionId = 0
					this.cityId = 0
					this.regionList = areaList[index].children[0].children
					this.cityList = areaList[index].children
					this.onSetAddress('province', dataObj)

					this.onSetAddress('city', {
						label: areaList[index].children[0].name,
						value: areaList[index].children[0].code,
					})

					this.onSetAddress('region', {
						label: areaList[provinceId].children[0].children[0].name,
						value: areaList[provinceId].children[0].children[0].code,
					})

				} else if (typeName == 'cityId') { //市
					this.regionList = areaList[provinceId].children[index].children
					this.regionId = 0
					this.onSetAddress('region', {
						label: areaList[provinceId].children[cityId].children[0].name,
						value: areaList[provinceId].children[cityId].children[0].code,
					})


					this.onSetAddress('city', dataObj)
				} else { //区
					this.onSetAddress('region', dataObj)

				}


			},


			goSelect() {

				this.$adminPost('AdminGetClassificationList', {
					type: 4
				}).then(res => {
					this.areaList = JSON.parse(res.data)
					let {
						areaList,
						provinceId,
						cityId,
						regionId
					} = this



					this.regionList = areaList[provinceId].children[cityId].children //区
					this.cityList = areaList[provinceId].children //市

					this.addressObj = {
						province: {
							label: areaList[provinceId].name,
							value: areaList[provinceId].code,
						},
						city: {
							label: areaList[provinceId].children[cityId].name,
							value: areaList[provinceId].children[cityId].code,
						},

						region: {
							label: areaList[provinceId].children[cityId].children[regionId].name,
							value: areaList[provinceId].children[cityId].children[regionId].code,
						},

					}
				})
				this.areaShow = true

			},


		}
	};
</script>

<style scoped lang="scss">
	.area_content {
		.area_content_top {
			display: flex;
			justify-content: space-between;

		}

		.sumbit_btn {
			width: 100%;
			height: 50rpx;
			margin-top: 60rpx;
			display: flex;
			justify-content: space-between;
			padding: 0 60rpx;
		}

		padding: 40rpx;
		width: 100%;
		height: 100%;
		font-size: 30rpx;
		text-align: center;
		background-color: #FFFFFF;

		.area_content_item {
			width: 200rpx;
			height: 300rpx;
			box-shadow: 0 0 5rpx 5rpx rgba(0, 0, 0, .05);
			// overflow-y: auto;

			.area_content_item_content {
				// margin-bottom: 10rpx;
				min-height: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				// line-height: 80rpx;
			}
		}
	}
</style>
